package com.study.mqTest.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class ConsumeListener {

    private static final String URL;

    private static final String USERNAME;

    private static final String PASSWORD;

    static {
        URL = "tcp://127.0.0.1:61616";
        USERNAME = "admin";
        PASSWORD = "admin";
    }

    public static void main(String[] args) {


        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(URL);

        try {

            Connection connection = activeMQConnectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            Queue myQueue01 = session.createQueue("myQueue01");
            MessageConsumer messageConsumer = session.createConsumer(myQueue01);

            messageConsumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message message) {

                    try {
                        if (message!=null && message instanceof TextMessage) {

                            TextMessage textMessage = (TextMessage) message;

                            System.out.println("一号消费者消费========：" + textMessage.getText());
                        }
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }

                }
            });

            System.in.read();
            messageConsumer.close();
            session.close();
            connection.close();

        }catch (Exception e){

            e.printStackTrace();
        }
    }

}




